using System;
using System.Collections.Generic;
using System.Text;

namespace HW2and3
{
    class NQStateQueen : StateInterface, ICloneable
    {
        public short[,] board;
        private int x;
        private int y;
        private short index;

        public NQStateQueen(int size, int x, int y, short index)
        {
            board = new short[size, size];
            this.x = x;
            this.y = y;
            this.index = index;
            board[this.x,this.y] = 1;
        }

        private void SetCoordinates(int x, int y)
        {
            for (int i = 0; i < board.Length; i++)
                if(board[i, y] == 0) board[i, y] = -1;
            for (int i = 0; i < board.Length; i++)
                if(board[i, y] == 0) board[i, y] = -1;

            for (int i = 0; (x+i) < board.Length && (y+i) < board.Length; i++)
                if(board[x+i, y+i] == 0) board[x+i, y+i] = -1;
            for (int i = 0; (x-i) >= 0 && (y+i) < board.Length; i++)
                if(board[x-i, y+i] == 0) board[x-i, y+i] = -1;
            for (int i = 0; (x-i) >= 0 && (y-i) > 0; i++)
                if(board[x-i, y-i] == 0) board[x-i, y-i] = -1;
            for (int i = 0; (x+i) < board.Length && (y-i) > 0; i++)
                if(board[x+i, y-i] == 0) board[x+i, y-i] = -1;

            board[this.x, this.y] = ++index;

            this.x = x;
            this.y = y;
        }

        public short[,] Board
        {
            get { return board; }
        }


        public int LastX
        {
            get { return x; }
        }

        public int LastY
        {
            get { return y; }
        }

        public short Index
        {
            get { return index; }
            set { index = value; }
        }

        public int X
        {
            get { return x; }
            set { x = value; }
        }

        public int Y
        {
            get { return y; }
            set { y = value; }
        }

        public NQStateQueen GetNewState(int x, int y)
        {
            NQStateQueen tmp = (NQStateQueen)this.Clone();
            tmp.SetCoordinates(x, y);
            return tmp;
        }

        public object Clone()
        {
            NQStateQueen tmp = new NQStateQueen(this.Board.Length, this.x, this.y, this.index);

            for (int i = 0; i < board.Length; i++)
            {
                for (int j = 0; j < board.Length; j++)
                {
                    tmp.board[i, j] = board[i, j];
                }
            }
            tmp.Index = this.index;
            tmp.X = this.x;
            tmp.Y = this.y;

            return (object)tmp;
        }
    }
}
